package servlet;

import util.Linksql;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class SaveServlet extends HttpServlet {
    String id=null;
    String password=null;
    String email=null;
    String phone=null;
    Connection conn=null;
    PreparedStatement pre=null;
    int n=0;

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String sql="update student set password="+"?"+",phone="+"?"+",email="+"?"+"where stuid="+"?";
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        try {
            id=request.getParameter("id");
            password=request.getParameter("password");
            phone=request.getParameter("phone");
            email=request.getParameter("email");
            conn= Linksql.getConnection();
            pre=conn.prepareStatement(sql);
            pre.setString(1,password);
            pre.setString(2,phone);
            pre.setString(3,email);
            pre.setString(4,id);
            n= pre.executeUpdate();
            if(n==0){
               response.getWriter().write("保存失败");
            }else {
                response.getWriter().write("保存成功");
            }
            response.sendRedirect("UserStuMessage.jsp");
            pre.close();
            conn.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
